//获取验证码
var i;
function send(){
    i--;
    if(i==0){
        $('#getVerifyCodeBtn').removeAttr("disabled");
        $('#getVerifyCodeBtn').text('获取验证码');
        return null;
    }
    $('#getVerifyCodeBtn').text(i+'秒');
    setTimeout("send();",1000);
}
$('#getVerifyCodeBtn').click(function(){
    var phone = $('#cli_tel').val();
    if(phone!=""){
        $.Dialog.loading();
        $.ajax({
            url:applyvip.sendPhoneCode + "&phone="+phone,
            type:"get",
            dataType:"json",
            success:function(data){
//                $.Dialog.success("发送成功!");
//                $('#getVerifyCodeBtn').attr('disabled',"true");
//                i = 101;
//                send();
                if(data==100){
                    $.Dialog.success("发送成功!");
                    $('#getVerifyCodeBtn').attr('disabled',"true");
                    i = 101;
                    send();
                }
                else{
                    $.Dialog.fail(data);
                }
            }
        });
    }else{
        $.Dialog.fail("请填写手机号！");
    }
});

/*加载多个会员卡*/
function initcardlist(data){
    $('.cardlist ul').html('');
    if(data){
        $.each(data, function(index, row){
            var li = $('<li class="p_10 arrow_icon mr_10"><b>卡号：</b><span></span>,<b>积分：</b><span></span></li>');
            li.find('span').eq(0).text(row.cli_card);
            li.find('span').eq(1).text(row.cli_card_jf);
            $('.cardlist ul').append(li);
        })
        $('.cardlist li').bind('click', function(){
            $('.morecard').hide();
            var cid = $(this).find('span').eq(0).text();
            $.ajax({
                url:applyvip.bindOneCard,
                type:"post",
                data:{cardid: cid},
                dataType:"json",
                success:function(data){
                    $.Dialog.success(data);
                    setTimeout(function(){
                        location.href = applyvip.showCardurl;
                    },2000);
                }
            })
        })
    }
}

$('#submit').click(function(){
    if($('#cli_name').val()!=undefined && $('#cli_name').val()==""){
        $.Dialog.fail("请填写姓名！");//成功调用 提示一秒后自动关闭
        return false;
    }
    if($('#cli_tel').val()!=undefined && $('#cli_tel').val()==""){
        $.Dialog.fail("请填写手机号！");//成功调用 提示一秒后自动关闭
        return false;
    }
    if($('#verifyCode').val()!=undefined && $('#verifyCode').val()==""){
        $.Dialog.fail("请填写验证码！");//成功调用 提示一秒后自动关闭
        return false;
    }
    else{
        $.Dialog.loading();
        var cli_name = $('#cli_name').val();
        var cli_tel = $('#cli_tel').val();
        var verifyCode = $('#verifyCode').val();
        $.ajax({
            url: applyvip.checkApply,
            type:"post",
            data:{cli_name: cli_name, cli_tel: cli_tel, vcode:verifyCode},
            dataType:"json",
            success:function(data){
                switch (data.statu){
                    case 0:
                        $.Dialog.success(data.data);
                        setTimeout(function(){
                            location.href = applyvip.showCardurl;
                        },2000);
                        break;
                    case 1:
                        $.Dialog.fail(data.data);
                        break;
                    case 2:
                        $.Dialog.close();
                        $('.morecard').show();
                        initcardlist(data.data);
                        break;
                }
            }
        });
    }
})